package com.datang.litejvm.constant;

import com.datang.litejvm.print.Visitor;

public class ConstantMethodRefInfo extends ConstantInfo {
    private int tag = ConstantInfo.CONSTANT_METHOD_REF_INFO;
    private int classIndex;
    private int nameAndTypeIndex;
    private String value = "Methodref";

    public ConstantMethodRefInfo(ConstantPool pool) {
        super(pool);
    }

    public int getTag() {
        return tag;
    }

    public int getClassIndex() {
        return classIndex;
    }

    public void setClassIndex(int classIndex) {
        this.classIndex = classIndex;
    }

    public int getNameAndTypeIndex() {
        return nameAndTypeIndex;
    }

    public void setNameAndTypeIndex(int nameAndTypeIndex) {
        this.nameAndTypeIndex = nameAndTypeIndex;
    }

    /**
     * @author: 顶风少年
     * @Description: 获取方法所属的类
     * @date: 22:19 2022/6/16
     **/
    public String getClassName() {
        ConstantClassInfo constantClassInfo = (ConstantClassInfo) constantPool.getConstantInfo(classIndex);
        return constantClassInfo.getClassName();
    }

    /**
     * @author: 顶风少年
     * @Description: 获取方法名
     * @date: 9:55 2022/6/17
     **/
    public String getMethodName() {
        ConstantNameAndTypeInfo constantNameAndTypeInfo = (ConstantNameAndTypeInfo) constantPool.getConstantInfo(nameAndTypeIndex);
        int methodNameIndex = constantNameAndTypeInfo.getNameIndex();
        ConstantUTF8Info constantUTF8Info = (ConstantUTF8Info) constantPool.getConstantInfo(methodNameIndex);
        return constantUTF8Info.getBytes();
    }

    /**
     * @author: 顶风少年
     * @Description: 获取返回值
     * @date: 9:58 2022/6/17
     **/
    public String getParamAndResult() {
        ConstantNameAndTypeInfo constantNameAndTypeInfo = (ConstantNameAndTypeInfo) constantPool.getConstantInfo(nameAndTypeIndex);
        int methodReturnIndex = constantNameAndTypeInfo.getDescriptorIndex();
        ConstantUTF8Info constantUTF8Info = (ConstantUTF8Info) constantPool.getConstantInfo(methodReturnIndex);
        return constantUTF8Info.getBytes();
    }

    @Override
    public String getValue() {
        return value;
    }

    @Override
    public void accept(Visitor visitor) {
        visitor.visit(this);
    }
}
